package ysq.xyg.sys.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.sys.dao.SysDictDao;
import ysq.xyg.sys.model.po.SysDict;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.sys.model.vo.SysDictVo;
import ysq.xyg.sys.service.SysDictService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


/**
 * 系统部分-字典service实现
 *
 * @author shengQiang yu
 * @date 2018-03-18 15:42
 */
@Service("baseDictService")
public class SysDictServiceImpl extends BaseServiceImpl<SysDict> implements SysDictService {

    @Resource
    private SysDictDao sysDictDao;

    private SysDict sysDict;

    @Autowired
    public void setBaseDao(SysDictDao sysDictDao) {
        this.baseDao = sysDictDao;
    }


    /**
     * 分页查询
     *
     * @param sysDictVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2018-03-18 16:26
     */
    public ReturnEntity page(SysDictVo sysDictVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        List<SysDict> list = acceptList(sysDictVo);
        PageInfo<SysDict> page=new PageInfo<SysDict>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param sysDictVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2018-03-18 16:26
     */
    public ReturnEntity addOrUpdate(SysDictVo sysDictVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(sysDictVo.getId())){
            sysDict=new SysDict(currentUser);
            isUpate=false;
        }else{
            sysDict=selectByPrimaryKey(sysDictVo.getId());
            if(ObjectUtil.isNull(sysDict)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            sysDict.setUpdateTime(new Date());
            sysDict.setUpdateUser(currentUser.getId());
        }
        sysDict.setName(sysDictVo.getName());
        sysDict.setCode(sysDictVo.getCode());
        sysDict.setStatus(sysDictVo.getStatus());
        sysDict.setRemark(sysDictVo.getRemark());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(sysDict);
        }else{
            num=insert(sysDict);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }
    /**
     * 删除
     *
     * @param id 主键id删除
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2018-03-18 16:26
     */
    public ReturnEntity delete(String id, SysUser currentUser) {
        SysDict sysDict=selectByPrimaryKey(id);
        if(sysDict!=null){
            sysDict.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            int num=updateByPrimaryKey(sysDict);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param sysDictVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author shengQiang yu
     * @date 2019-03-18 16:20
     */
    public List<SysDict> acceptList(SysDictVo sysDictVo){
        Example example = new Example(SysDict.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(sysDictVo.getName())){
            criteria.andLike("name","%" + sysDictVo.getName() + "%");
        }
        if(StringUtil.isNotEmpty(sysDictVo.getCode())){
            criteria.andLike("code","%" + sysDictVo.getCode() + "%");
        }
        if(StringUtil.isNotEmpty(sysDictVo.getStatus())){
            criteria.andEqualTo("status",sysDictVo.getStatus());
        }
        if(StringUtil.isNotEmpty(sysDictVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",sysDictVo.getRemoveTag());
        }
        example.setOrderByClause(" update_time desc");
        return sysDictDao.selectByExample(example);
    }
}
